package com.smedi.ismedi.personnel.core.port.adapter.service.impl;

import com.smedi.ismedi.constructionproject.api.dto.request.ProjectAssignmentListQueryRequest;
import com.smedi.ismedi.constructionproject.api.dto.request.ProjectAssignmentRequest;
import com.smedi.ismedi.constructionproject.api.dto.request.ProjectListQueryRequest;
import com.smedi.ismedi.constructionproject.api.dto.response.ProjectAssignmentResponse;
import com.smedi.ismedi.constructionproject.api.dto.response.ProjectBasicResponse;
import com.smedi.ismedi.constructionproject.api.dto.response.resp.ProjectAssignmentResp;
import com.smedi.ismedi.constructionproject.api.feign.RemoteConstructionProjectFeignApi;
import com.smedi.ismedi.constructionproject.api.feign.RemoteProjectAssignmentFeignApi;
import com.smedi.ismedi.personnel.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.personnel.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.personnel.core.port.adapter.persistence.dto.ProjectAssignmentQuery;
import com.smedi.ismedi.personnel.core.port.adapter.service.ConstructionProjectRemoteApiService;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-6-24 16:40
 */
@Slf4j
@Service
@AllArgsConstructor
public class ConstructionProjectRemoteApiServiceImpl implements ConstructionProjectRemoteApiService {
    private final RemoteProjectAssignmentFeignApi remoteProjectAssignmentFeignApi;
    private final RemoteConstructionProjectFeignApi remoteConstructionProjectFeignApi;
    private final AdapterConverter adapterConverter;

    @Override
    public ProjectAssignmentResponse saveProjectAssignment(ProjectAssignmentRequest projectAssignmentRequest) {
        ResponseData<ProjectAssignmentResponse> response = remoteProjectAssignmentFeignApi.postSave(projectAssignmentRequest);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        } else {
            return response.getData();
        }
    }

    @Override
    public List<ProjectAssignmentResponse> getProjectAssignmentsByPersonId(Long personId) {
        ResponseData<List<ProjectAssignmentResponse>> response = remoteProjectAssignmentFeignApi.getQueryProjectAssignmentsByPersonId(personId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public Boolean checkProjectAvailable(Long projectId) {
        ResponseData<Boolean> response = remoteConstructionProjectFeignApi.getCheckProjectAvailable(projectId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public void deleteProjectAssignmentsByPersonId(Long personId) {
        ResponseData<Boolean> response = remoteProjectAssignmentFeignApi.deleteProjectAssignmentsByPersonId(personId);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<ProjectBasicResponse> getQueryProjectBasicListByPersonId(Long personId) {
        ResponseData<List<ProjectBasicResponse>> response = remoteConstructionProjectFeignApi.getQueryProjectBasicListByPersonId(personId);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        } else {
            return response.getData();
        }
    }

    @Override
    public List<ProjectBasicResponse> getQueryProjectBasicListByHrOrganizationIds(List<Long> hrOrganizationIds) {
        ResponseData<List<ProjectBasicResponse>> response = remoteConstructionProjectFeignApi.getQueryProjectBasicListByHrOrganizationIds(hrOrganizationIds);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        } else {
            return response.getData();
        }
    }

    @Override
    public List<ProjectBasicResponse> getQueryAllProjectBasicList() {
        ResponseData<List<ProjectBasicResponse>> response = remoteConstructionProjectFeignApi.getQueryAvailableProjectBasicList();
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        } else {
            return response.getData();
        }
    }

    @Override
    public ProjectBasicResponse getProjectBasicByProjectId(Long projectId) {
        ResponseData<ProjectBasicResponse> response = remoteConstructionProjectFeignApi.getQueryProjectBasicView(projectId);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        } else {
            return response.getData();
        }
    }

    @Override
    public List<Long> getQueryPersonIdsByProjectId(Long projectId) {
        ResponseData<List<Long>> response = remoteProjectAssignmentFeignApi.getQueryPersonIdsByProjectId(projectId);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        } else {
            return response.getData();
        }
    }

    @Override
    public List<ProjectAssignmentResp> getProjectAssignmentList(ProjectAssignmentQuery projectAssignmentQuery) {
        ProjectAssignmentListQueryRequest projectAssignmentListQueryRequest = adapterConverter.projectAssignmentQueryToProjectAssignmentListQueryRequest(projectAssignmentQuery);
        ResponseData<List<ProjectAssignmentResp>> response = remoteProjectAssignmentFeignApi.getProjectAssignmentList(projectAssignmentListQueryRequest);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        } else {
            return response.getData();
        }
    }

    @Override
    public List<ProjectAssignmentResp> getProjectAssignmentListWithoutToken(ProjectAssignmentQuery projectAssignmentQuery) {
        ProjectAssignmentListQueryRequest projectAssignmentListQueryRequest = adapterConverter.projectAssignmentQueryToProjectAssignmentListQueryRequest(projectAssignmentQuery);
        ResponseData<List<ProjectAssignmentResp>> response = remoteProjectAssignmentFeignApi.getProjectAssignmentListWithoutToken(projectAssignmentListQueryRequest);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        } else {
            return response.getData();
        }
    }
}
